package com.replayingDecoder;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

public class HeaderFrameDecoder extends ReplayingDecoder<Void> {

	@Override
	protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
		// 读取到头的长度length
		int length = in.readInt();
		// 如果用ByteToMessageDecoder，这里需要自己判断是否可以读取到length个字节
		out.add(in.readBytes(length));
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		cause.printStackTrace();
    }

}
